package com.huawei.hwid.core.b.a.a;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.cloudservice.CloudRequestHandler;
import com.huawei.hms.framework.common.ContainerUtils;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hwid.core.b.a.a.c;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.n;
import com.huawei.hwid.core.d.p;
import com.huawei.hwid.core.datatype.DeviceInfo;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import com.huawei.hwid.f.g;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

public class f extends com.huawei.hwid.core.b.a.a {
    private String h;
    private String i = "0";
    private String j;
    private String k = "";
    private String l = HwAccountConstants.TYPE_TENCENT;
    private String m;
    private String n;
    private String o;
    private String p;
    private String q;
    private String r;
    private String s;
    private Context t;
    private int u;

    static class a extends com.huawei.hwid.core.helper.handler.a {
        private CloudRequestHandler a;

        public a(Context context, CloudRequestHandler cloudRequestHandler) {
            super(context);
            this.a = cloudRequestHandler;
        }

        public void a(Bundle bundle) {
            super.a(bundle);
            bundle.putBoolean(HwAccountConstants.SERVICETOKENAUTH_IS_SECCUSS, true);
            this.a.onFinish(bundle);
        }

        public void b(Bundle bundle) {
            super.b(bundle);
            ErrorStatus errorStatus = (ErrorStatus) bundle.getParcelable("requestError");
            if (errorStatus != null) {
                this.a.onError(errorStatus);
            } else {
                this.a.onError(new ErrorStatus(32, "ErrorStatus is null"));
            }
        }
    }

    public f(Context context, String str, String str2, int i, String str3, String str4) {
        this.r = p() ? "/IUserInfoMng/stAuth" : "/IUserInfoMng/serviceTokenAuth";
        this.s = this.r;
        this.u = 2;
        this.t = context;
        if ("com.huawei.hwid".equalsIgnoreCase(str) || TextUtils.isEmpty(str)) {
            str = "com.huawei.hwid";
        }
        this.g = 0;
        a(c.URLType);
        h(str2);
        f(context.getPackageName());
        if (TextUtils.isEmpty(str3)) {
            i(n.a(context, 0));
        } else {
            i(str3);
        }
        if (TextUtils.isEmpty(str4)) {
            g(HwAccountConstants.TYPE_SECURITY_PHONE);
        } else {
            g(str4);
        }
        j(n.a(context, 0));
        a(context, i);
        c(this.u);
        k(com.huawei.hwid.core.d.a.a(context, str));
        a(true);
    }

    public String j() throws IllegalArgumentException, IllegalStateException, IOException {
        OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            XmlSerializer a = p.a(byteArrayOutputStream);
            a.startDocument("UTF-8", Boolean.valueOf(true));
            a.startTag(null, "ServiceTokenAuthReq");
            p.a(a, "version", "27120");
            p.a(a, "serviceToken", this.j);
            p.a(a, "appID", TextUtils.isEmpty(this.k) ? "com.huawei.hwid" : this.k);
            a.startTag(null, "deviceInfo");
            p.a(a, DeviceInfo.TAG_DEVICE_ID, this.n);
            p.a(a, "deviceType", this.m);
            p.a(a, "terminalType", n.b());
            a.endTag(null, "deviceInfo");
            p.a(a, CloudAccountManager.KEY_REQCLIENTTYPE, this.l);
            p.a(a, "clientIP", "");
            p.a(a, CloudAccountManager.KEY_LOGIN_CHANNEL, this.p);
            p.a(a, "uuid", this.o);
            p.a(a, "chkAcctChange", "0");
            p.a(a, "isGetAccount", "0");
            p.a(a, "isGetAgrVers", this.i);
            a.endTag(null, "ServiceTokenAuthReq");
            a.endDocument();
            String byteArrayOutputStream2 = byteArrayOutputStream.toString("UTF-8");
            return byteArrayOutputStream2;
        } finally {
            try {
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.d("ServiceTokenAuthRequest", "IOException", true);
            }
        }
    }

    public void a(String str) throws XmlPullParserException, IOException {
        XmlPullParser a = p.a(str.getBytes("UTF-8"));
        for (int eventType = a.getEventType(); 1 != eventType; eventType = a.next()) {
            String name = a.getName();
            switch (eventType) {
                case 2:
                    if (HiAnalyticsConstant.BI_KEY_RESUST.equals(name)) {
                        this.b = g.a(a.getAttributeValue(null, "resultCode"));
                    }
                    if (this.b != 0) {
                        if (!"errorCode".equals(name)) {
                            if (!"errorDesc".equals(name)) {
                                break;
                            }
                            this.d = a.nextText();
                            break;
                        }
                        this.c = g.a(a.nextText());
                        break;
                    }
                    break;
                default:
                    break;
            }
        }
    }

    public String k() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("ver=").append("27120").append(ContainerUtils.FIELD_DELIMITER).append("st=").append(this.j).append(ContainerUtils.FIELD_DELIMITER).append("app=").append(TextUtils.isEmpty(this.k) ? "com.huawei.hwid" : this.k).append(ContainerUtils.FIELD_DELIMITER).append("dvT=").append(this.m).append(ContainerUtils.FIELD_DELIMITER).append("dvID=").append(t()).append(ContainerUtils.FIELD_DELIMITER).append("tmT=").append(n.a()).append(ContainerUtils.FIELD_DELIMITER).append("clT=").append(this.l).append(ContainerUtils.FIELD_DELIMITER).append("cn=").append(this.p).append(ContainerUtils.FIELD_DELIMITER).append("chg=").append("0").append(ContainerUtils.FIELD_DELIMITER).append("gAc=").append("0").append(ContainerUtils.FIELD_DELIMITER).append("uuid=").append(this.o).append(ContainerUtils.FIELD_DELIMITER).append("agr=").append(this.i).append("&lang=").append(c(this.t));
        return stringBuffer.toString();
    }

    public void b(String str) {
        if (!TextUtils.isEmpty(str)) {
            String[] split = str.split(ContainerUtils.FIELD_DELIMITER);
            HashMap hashMap = new HashMap();
            Object obj = "";
            for (String split2 : split) {
                String[] split3 = split2.split(ContainerUtils.KEY_VALUE_DELIMITER);
                hashMap.put(split3[0], split3[1]);
                obj = split3[0];
            }
            if (hashMap.containsKey("resultCode")) {
                this.b = g.a((String) hashMap.get("resultCode"));
            }
            if (this.b == 0) {
                this.q = (String) hashMap.get("userID");
                this.h = (String) hashMap.get("agrFlags");
                return;
            }
            this.c = this.b;
            this.d = (String) hashMap.get(obj);
        }
    }

    public String b() {
        return this.s;
    }

    private void f(String str) {
        this.k = str;
    }

    private void g(String str) {
        this.m = str;
    }

    private void h(String str) {
        this.j = str;
    }

    private void i(String str) {
        this.n = str;
    }

    private void j(String str) {
        this.o = str;
    }

    private void k(String str) {
        this.p = str;
    }

    public String s() {
        return this.q;
    }

    public Bundle c() {
        Bundle c = super.c();
        c.putString("agrFlags", this.h);
        return c;
    }

    public String c(Context context) {
        return b.f(context);
    }

    private String t() {
        String str = "";
        if (!"NULL".equals(this.n) && !TextUtils.isEmpty(this.n)) {
            return this.n;
        }
        if ("NULL".equals(this.o) || TextUtils.isEmpty(this.o)) {
            return str;
        }
        return this.o;
    }

    public void a(Context context, com.huawei.hwid.core.b.a.a aVar, String str, CloudRequestHandler cloudRequestHandler) {
        com.huawei.hwid.core.b.a.e.a(context, aVar, str, a(context, aVar, new a(context, cloudRequestHandler)));
    }
}
